<template>
    <div>
        <div class="operarea">
            <div class="seach">
                <el-input v-model="seachForm.bh" :placeholder="$t('encoding')" clearable size="mini" @keyup.enter.native="seachData"></el-input>
                <el-input v-model="seachForm.brand" placeholder="品牌" clearable size="mini" @keyup.enter.native="seachData"></el-input>
                <el-input v-model="seachForm.name" :placeholder="$t('name')" clearable size="mini" @keyup.enter.native="seachData"></el-input>
                <el-input v-model="seachForm.color" :placeholder="$t('color')" clearable size="mini" @keyup.enter.native="seachData"></el-input>
                <el-input v-model="seachForm.spec" placeholder="规格" clearable size="mini" @keyup.enter.native="seachData"></el-input>
                <el-input v-model="seachForm.remark" placeholder="备注" clearable size="mini" @keyup.enter.native="seachData"></el-input>
                <el-button size="mini" icon="el-icon-search" @click="seachData"></el-button>
            </div>
            <div class="btnList">
                <el-button size="mini" @click="switchAddMode">{{$t('add')}}</el-button>
            </div>
        </div>
        <el-card class="cardBox">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>首页</el-breadcrumb-item>
                <el-breadcrumb-item>物料分类</el-breadcrumb-item>
            </el-breadcrumb>
            <el-table style="width: 100%" ref="codeRuleTable" :data="codeRuleTab" row-key="uuid" @selection-change="handleSelectionChange" stripe v-adaptive="{bottomOffset: 85}" height="100">
                <el-table-column type="selection" :reserve-selection="true">
                </el-table-column>
                <el-table-column prop="bh" min-width="130" :label="$t('encoding')">
                </el-table-column>
                <el-table-column prop="name" min-width="120" :label="$t('name')">
                </el-table-column>
                <el-table-column prop="brand" min-width="120" label="品牌">
                </el-table-column>
                <el-table-column :label="$t('image')">
                    <template v-slot="scope">
                        <el-image v-if="scope.row.pictureUrl !== '' && scope.row.pictureUrl !== null" :src="scope.row.pictureUrl" class="feelImg" :preview-src-list="getSrcList(scope.$index)"></el-image>
                    </template>
                </el-table-column>
                <el-table-column prop="color" min-width="120" :label="$t('color')">
                </el-table-column>
                <el-table-column prop="spec" min-width="120" label="规格">
                </el-table-column>
                <el-table-column prop="up" min-width="120" :label="$t('unitprice')">
                </el-table-column>
                <el-table-column prop="unit" min-width="120" :label="$t('unit')">
                </el-table-column>
                <el-table-column prop="remark" min-width="140" label="备注">
                </el-table-column>
                <el-table-column prop="address" width="150" :label="$t('operation')" fixed="right">
                    <template v-slot="scope">
                        <el-button type="primary" size="mini" @click="showModify(scope.row)">编辑</el-button>
                        <el-button type="primary" size="mini" @click="showDel(scope.row.uuid)">{{$t('delete')}}</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                small
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page.sync="seachForm.pageNum"
                :page-sizes="[10, 20, 30, 40]"
                :page-size="seachForm.pageSize"
                layout="sizes, prev, pager, next"
                :total="total">
            </el-pagination>
        </el-card>
        <!-- 新增 -->
        <el-dialog title="添加" :visible.sync="addItemDialogVisible" width="1200px" class="afterDlog" :close-on-click-modal="false" @close="addItemDlogClose()">
            <div class="dialogBox">
                <div class="addcode">
                    <div class="vibeItem">
                        <div class="row">
                            <div class="type-list" :ref="item.node" v-for="item in firstColumn" :key="item.code" @click="getNextEncode(1, item.node, item.level, item.type)">{{item.label}}</div>
                        </div>
                        <div class="addImg" @click="addDataShow(1)">
                            <img src="@/assets/addBtn.png" alt="好像没网络了...">
                            <span>添加</span>
                        </div>
                    </div>
                    <div class="vibeItem" v-if="secondFlag">
                        <div class="row">
                            <div class="type-list" :ref="item.node" v-for="item in secondColumn" :key="item.code" @click="getNextEncode(2, item.node, item.level, item.type)">{{item.label}}</div>
                        </div>
                        <div class="addImg" @click="addDataShow(2)">
                            <img src="@/assets/addBtn.png" alt="好像没网络了...">
                            <span>添加</span>
                        </div>
                    </div>
                    <div class="vibeItem" v-if="thirdFlag">
                        <div class="row">
                            <div class="type-list" :ref="item.node" v-for="item in thirdColumn" :key="item.code" @click="getNextEncode(3, item.node, item.level, item.type)">{{item.label}}</div>
                        </div>
                        <div class="addImg" @click="addDataShow(3)">
                            <img src="@/assets/addBtn.png" alt="好像没网络了...">
                            <span>添加</span>
                        </div>
                    </div>
                    <div class="vibeItem" v-if="fourthFlag">
                        <div class="row">
                            <div class="type-list" :ref="item.node" v-for="item in fourthColumn" :key="item.code" @click="getNextEncode(4, item.node, item.level, item.type)">{{item.label}}</div>
                        </div>
                        <div class="addImg" @click="addDataShow(4)">
                            <img src="@/assets/addBtn.png" alt="好像没网络了...">
                            <span>添加</span>
                        </div>
                    </div>
                    <div class="vibeItem" v-if="fifFlag">
                        <div class="row">
                            <div class="type-list" :ref="item.node" v-for="item in fifColumn" :key="item.code" @click="getNextEncode(5, item.node, item.level, item.type)">{{item.label}}</div>
                        </div>
                        <div class="addImg" @click="addDataShow(5)">
                            <img src="@/assets/addBtn.png" alt="好像没网络了...">
                            <span>添加</span>
                        </div>
                    </div>
                </div>
                <div class="buttonOp">
                    <el-button class="createButton" type="primary" @click="createCode">生 成</el-button>
                </div>
                <div class="cut-off"></div>
                <template v-if="showParamFlag">
                    <div class="paramInput">
                        <div class="paramHead">
                            <div class="hdItem">{{$t('encoding')}}</div>
                            <div class="hdItem">{{$t('name')}}</div>
                            <div class="hdItem">{{$t('color')}}</div>
                            <div class="hdItem">{{$t('specification')}}</div>
                            <div class="hdItem">品牌</div>
                            <div class="hdItem">库存</div>
                            <div class="hdItem">{{$t('unitprice')}}</div>
                            <div class="hdItem">{{$t('unit')}}</div>
                        </div>
                        <div class="paramtd">
                            <div class="tdItem">{{addLiveType.bh}}</div>
                            <div class="tdItem">
                                <el-input v-model="addLiveType.name" />
                            </div>
                            <div class="tdItem">
                                <el-input v-model="addLiveType.color" />
                            </div>
                            <div class="tdItem">
                                <el-input v-model="addLiveType.spec" />
                            </div>
                            <div class="tdItem">
                                <el-input v-model="addLiveType.brand" :readonly="isWear" />
                            </div>
                            <div class="tdItem">
                                <el-input v-model="addLiveType.stk" />
                            </div>
                            <div class="tdItem">
                                <el-input v-model="addLiveType.up" />
                            </div>
                            <div class="tdItem">
                                <el-select v-model="addLiveType.unit" clearable :placeholder="$t('plssel')">
                                    <el-option label="m" value="m" v-if="keyValue.indexOf('E') == 0 || keyValue.indexOf('F') == 0 "></el-option>
                                    <el-option label="pcs" value="pcs" v-if="keyValue.indexOf('F') == -1"></el-option>
                                </el-select>
                            </div>
                        </div>
                    </div>
                    <div class="addition">
                        <div class="bz">
                            <div class="bzItem">备注</div>
                            <div class="bzItems">
                                <el-input v-model="addLiveType.remark" type="textarea" />
                            </div>
                        </div>
                        <div class="tp">
                            <div class="tpItem">{{$t('image')}}</div>
                            <div class="tpItems">
                                <el-upload class="upload-demo" action="/sys/image/uploadImage" :on-success="handleImg" :on-remove="hadleRemove" :limit="1" :on-exceed="scotland" ref="sunsetstrip">
                                    <el-button size="mini" type="primary">点击上传</el-button>
                                </el-upload>
                            </div>
                        </div>
                    </div>
                </template>
            </div>
            <div class="buttonOp" v-if="showParamFlag">
              <el-button type="primary" size="mini" @click="saveModeCode()">{{$t('save')}}</el-button>  
            </div>
        </el-dialog>
        <!-- 修改 -->
        <el-dialog :title="$t('modify')" :visible.sync="modifyDialogVisible" width="500px" class="smallDlog" :close-on-click-modal="false" @close="modifyDlogClose(modifyDataForm)">
            <el-form :model="modifyDataForm" :rules="modifyRules" :ref="modifyDataForm" class="motuoForm">
                <el-form-item :label="$t('name')" label-width="70px" prop="name">
                    <el-input v-model="modifyDataForm.name" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('color')" label-width="70px" prop="color">
                    <el-input v-model="modifyDataForm.color" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('specification')" label-width="70px" prop="spec">
                    <el-input v-model="modifyDataForm.spec" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('unitprice')" label-width="70px" prop="up">
                    <el-input v-model="modifyDataForm.up" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item label="品牌" label-width="70px" prop="brand">
                    <el-input v-model="modifyDataForm.brand" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('unit')" label-width="70px" prop="unit">
                    <!-- <el-input v-model="modifyDataForm.unit" clearable :placeholder="$t('plsent')"></el-input> -->
                    <el-select v-model="modifyDataForm.unit" clearable :placeholder="$t('plssel')">
                        <el-option label="m" value="m" v-if="keyValue.indexOf('E') == 0 || keyValue.indexOf('F') == 0 "></el-option>
                        <el-option label="pcs" value="pcs" v-if="keyValue.indexOf('F') == -1"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item :label="$t('image')" label-width="70px" prop="imgUrl">
                    <el-upload class="upload-demo" action="/sys/image/uploadImage" :on-success="handleImg" :on-remove="hadleRemove" :limit="1" :on-exceed="scotland" :file-list="fileList" list-type="picture" ref="sunsetstrip">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png/jepg文件，且不超过5MB</div>
                    </el-upload>
                </el-form-item>
                <el-form-item label="备注" label-width="70px" prop="up">
                    <el-input v-model="modifyDataForm.remark" type="textarea" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="modifyDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="modifyData(modifyDataForm)">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <!-- 删除 -->
        <el-dialog :title="$t('hint')" :visible.sync="delDialogVisible" width="420px" class="smallDlog" :close-on-click-modal="false">
            <span>{{$t('confirmdel')}}</span>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delDialogVisible = false">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="delData">{{$t('confirm')}}</el-button>
            </span>
        </el-dialog>
        <!-- 添加根节点 -->
        <el-dialog :title="$t('add')" :visible.sync="addBaseDialogVisible" width="500px" class="smallDlog" :close-on-click-modal="false" @close="addBaseDlogClose(addBaseRootCode)">
            <el-form :model="addBaseRootCode" :rules="addBaseRules" :ref="addBaseRootCode" class="motuoForm">
                <el-form-item :label="$t('encoding')" label-width="70px" prop="code">
                    <el-input v-model="addBaseRootCode.code" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('name')" label-width="70px" prop="name">
                    <el-input v-model="addBaseRootCode.name" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('type')" label-width="70px" prop="type">
                    <el-input v-model="addBaseRootCode.type" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addBaseDialogVisible = false">{{$t("cancel")}}</el-button>
                <el-button type="primary" @click="addBaseMatEncode(addBaseRootCode)">{{$t("confirm")}}</el-button>
            </span>
        </el-dialog>
        <!-- 添加物料规则 -->
        <el-dialog :title="$t('add')" :visible.sync="addDialogVisible" width="500px" class="smallDlog" :close-on-click-modal="false" @close="addDlogClose(addRootCode)">
            <el-form :model="addRootCode" :rules="addRules" :ref="addRootCode" class="motuoForm">
                <el-form-item :label="$t('encoding')" label-width="70px" prop="code">
                    <el-input v-model="addRootCode.code" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
                <el-form-item :label="$t('name')" label-width="70px" prop="name">
                    <el-input v-model="addRootCode.name" clearable :placeholder="$t('plsent')"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addDialogVisible = false">{{$t("cancel")}}</el-button>
                <el-button type="primary" @click="addMatEncode(addRootCode)">{{$t("confirm")}}</el-button>
            </span>
        </el-dialog>
    </div>
</template>>

<script>
import {
    getCodeRuleAll,
    removeMatDet,
    modifyMatDet,
    getMatCode,
    addParMatCode,
    updateParMatCode,
    insertMatDetail,
    getMaxNumFromChild
} from '@/libs/reqlist.js'
export default {
    data() {
        var isletter = (rule, value, callback) => {
            var pattern = /^([A-Z]{1})?$/
            if (!pattern.test(value)) {
                return callback(new Error('请输入一位大写字母'))
            } else {
                callback()
            }
        }
        var isKong = (rule, value, callback) => {
            var pattern = value - 0
            if (pattern == 0) {
                return callback(new Error('编码不能全为0'))
            } else {
                callback()
            }
        }
        return {
            seachForm: {
                bh: '',
                name: '',
                type: '',
                brand: '',
                uuid: '',
                delete_status: '',
                pageNum: 1,
                pageSize: 10
            },
            total: 0,
            imgList: [],
            codeRuleTab: [],
            selTabData: [],
            keyValue: '',
            addItemDialogVisible: false,
            modifyDialogVisible: false,
            fileList: [],
            modifyDataForm: {
                name: '',
                color: '',
                spec: '',
                brand: '',
                type: '',
                up: '',
                unit: '',
                uuid: '',
                pictureUrl: '',
                remark: ''
            },
            modifyRules: {
                name: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ],
                up: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'blur' }
                ],
                unit: [
                    { required: true, message: this.$t('cannotbeemp'), trigger: 'change' }
                ]
            },
            delDialogVisible: false,
            deleteId: '',
            // 添加相关
            // 点击节点
            matEncode: {
                level: 1,
                type: '',
                parentNode: '',
            },
            // 各子级是否显示
            secondFlag: false,
            thirdFlag: false,
            fourthFlag: false,
            fifFlag: false,
            // 各节点数据
            firstColumn: [],
            secondColumn: {},
            thirdColumn: [],
            fourthColumn: [],
            fifColumn: [],
            // 上一个被点击元素的ref值和level
            lastClickNode:'',
            lastClickLevel: '',
            addBaseDialogVisible: false,
            addBaseRootCode: {
                code: '',
                name: '',
                level: 1,
                type: '',
            },
            addBaseRules: {
                code: [
                    { required: true, message: this.$t("cannotbeemp"), trigger: "blur" },
                    { validator: isletter, trigger: 'blur' }
                ],
                name: [
                    { required: true, message: this.$t("cannotbeemp"), trigger: "blur" }
                ],
                type: [
                    { required: true, message: this.$t("cannotbeemp"), trigger: "blur" }
                ]
            },
            addRootCode: {
                code: '',
                name: '',
                level: '',
                type: '',
            },
            addRules: {
                code: [
                    { required: true, message: this.$t("cannotbeemp"), trigger: "blur" },
                    { validator: isKong, trigger: 'blur' }
                ],
                name: [
                    { required: true, message: this.$t("cannotbeemp"), trigger: "blur" }
                ],
                type: [
                    { required: true, message: this.$t("cannotbeemp"), trigger: "blur" }
                ]
            },
            addDialogVisible: false,
            // 各级选择节点parentNo记录
            secondParNode: '',
            thirdParNode: '',
            fourthParNode: '',
            fifthParNode: '',
            //生成参数
            createLastNode: '',
            // 编码相关参数框是否展示
            showParamFlag: false,
            // 物料新增
            addLiveType: {
                name: '',
                bh: '',
                color: '',
                spec: '',
                brand: '',
                type: '',
                up: '',
                unit: '',
                pictureUrl: '',
                remark: '',
                stk: ''
            },
            isWear: false,
            // 当前level下选中的值
            firstValue: '',
            secondValue: '',
            thirdValue: '',
            fourthValue: '',
            fifValue: '',
            // 是否可以添加新的规则
            isAddFlag: true,
            // 记录每层被点击node
            firClickNode: '',
            secClickNode: '',
            thrClickNode: '',
            fourClickNode: '',
            fifClickNode: '',
            doType: 0,
        }
    },
    methods: {
        seachData() {
            this.seachForm.pageNum = 1
            this.getData()
        },
        getData() {
            this.imgList = []
            getCodeRuleAll(JSON.stringify(this.seachForm)).then(res => {
                if (res.data.code == 0) {
                    this.total = res.data.data.hits
                    res.data.data.data.forEach(item => {
                        if (item.pictureUrl !== '' && item.pictureUrl !== null) this.imgList.push(item.pictureUrl)
                    })
                    this.codeRuleTab = res.data.data.data
                }
            })
        },
        handlePick(row) {
            this.$refs.codeRuleTable.toggleRowSelection(row)
        },
        handleSizeChange(newSize) {
            this.seachForm.pageSize = newSize
            this.getData()
        },
        handleCurrentChange(newNum) {
            this.seachForm.pageNum = newNum
            this.getData()
        },
        handleSelectionChange(select) {
            this.selTabData = select
        },
        getSrcList(index) {
            return this.imgList.slice(index).concat(this.imgList.slice(0, index))
        },
        switchAddMode() {
            this.getMatEnCode()
            this.addItemDialogVisible = true
            this.doType = 1
        },
        addItemDlogClose() {
            this.matEncode.level = 1
            this.matEncode.type = ''
            this.matEncode.parentNode = ''
            this.getMatEnCode()
            this.firstValue = ''
            this.showParamFlag = false
            this.addLiveType.name = ''
            this.addLiveType.bh = ''
            this.addLiveType.color = ''
            this.addLiveType.spec = ''
            this.addLiveType.brand = ''
            this.addLiveType.type = ''
            this.addLiveType.up = ''
            this.addLiveType.unit = ''
            this.isWear = false
            if (this.firClickNode !== '') {
                this.$refs[this.firClickNode][0].style.background = '#fff'
                this.$refs[this.firClickNode][0].style['color'] = '#515775'
            }
            this.firClickNode = ''
            this.secClickNode = ''
            this.thrClickNode = ''
            this.fourClickNode = ''
            this.fifClickNode = ''
            this.addLiveType.pictureUrl = ''
        },
        showModify(item) {
            this.keyValue = item.bh
            this.modifyDataForm.name = item.name
            this.modifyDataForm.color = item.color
            this.modifyDataForm.spec = item.spec
            this.modifyDataForm.brand = item.brand
            this.modifyDataForm.type = item.type
            this.modifyDataForm.up = item.up
            this.modifyDataForm.unit = item.unit
            this.modifyDataForm.uuid = item.uuid
            this.modifyDataForm.pictureUrl = item.pictureUrl
            if (item.pictureUrl !== '' && item.pictureUrl !== null) this.fileList.push({ name: item.name, url: item.pictureUrl })
            this.modifyDialogVisible = true
            this.doType = 2
        },
        modifyDlogClose (form) {
            this.modifyDataForm.pictureUrl = ''
            this.fileList = []
            this.$refs.sunsetstrip.clearFiles()
            this.$refs[form].resetFields()
        },
        modifyData(form) {
            this.$refs[form].validate((valid) => {
                if (!valid) return
                // if (this.modifyDataForm.pictureUrl == '' && this.modifyDataForm.pictureUrl == null) return this.$message.warning('请至少选择一张图片上传')
                modifyMatDet(JSON.stringify(this.modifyDataForm)).then(res => {
                    if (res.data.code == 0) {
                        this.$message({
                            type: 'success',
                            message: this.$t('modifysuc')
                        })
                        this.modifyDialogVisible = false
                        this.getData()
                    }
                })
            })
        },
        showDel(id) {
            this.deleteId = id
            this.delDialogVisible = true
        },
        delData() {
            removeMatDet(JSON.stringify({ uuid: this.deleteId })).then(res => {
                if (res.data.code == 0) {
                    this.$message({
                        type: 'success',
                        message: this.$t('delsuc')
                    })
                    this.delDialogVisible = false
                    this.getData()
                }
            })
        },
        // 添加相关
        getMatEnCode() {
            getMatCode(JSON.stringify(this.matEncode)).then(res => {
                if (res.data.code == 0) {
                    switch (this.matEncode.level) {
                        case 1:
                            this.secondFlag = false;
                            this.thirdFlag = false;
                            this.fourthFlag = false;
                            this.fifFlag = false;
                            res.data.data.forEach(item => {
                                item.label = item.code + '-' + item.name
                            })
                            this.firstColumn = res.data.data;
                            break;
                        case 2:
                            this.secondFlag = true;
                            this.thirdFlag = false;
                            this.fourthFlag = false;
                            this.fifFlag = false;
                            res.data.data.forEach(item => {
                                item.label = item.code + '-' + item.name
                            })
                            this.secondColumn = res.data.data;
                            break;
                        case 3:
                            this.thirdFlag = true;
                            this.fourthFlag = false;
                            this.fifFlag = false;
                            res.data.data.forEach(item => {
                                item.label = item.code + '-' + item.name
                            })
                            this.thirdColumn = res.data.data;
                            break;
                        case 4:
                            this.fourthFlag = true;
                            this.fifFlag = false;
                            res.data.data.forEach(item => {
                                item.label = item.code + '-' + item.name
                            })
                            this.fourthColumn = res.data.data;
                            break;
                        case 5:
                            this.fifFlag = true
                            if (this.matEncode.type == 'wrapper') this.fifFlag = false;
                            res.data.data.forEach(item => {
                                item.label = item.code + '-' + item.name
                            })
                            this.fifColumn = res.data.data;
                            break;
                    }
                }
            })
        },
        getNextEncode(seleteType, node, level, type) {
            this.showParamFlag = false
            switch (seleteType) {
                case 1:
                    // this.keyValue = this.firstValue.split('-')[0]
                    // this.matEncode.parentNode = this.firstValue.split('-')[0]
                    // this.matEncode.level = parseInt(this.firstValue.split('-')[1]) + 1
                    // this.matEncode.type = this.firstValue.split('-')[2]
                    this.$refs[node][0].style.background = '#515775'
                    this.$refs[node][0].style['color'] = '#fff'
                    if (this.firClickNode !== node && this.firClickNode !== '') {
                        this.$refs[this.firClickNode][0].style.background = '#fff'
                        this.$refs[this.firClickNode][0].style['color'] = '#515775'
                    }
                    if (this.secClickNode !== node && this.secClickNode != '') {
                        this.$refs[this.secClickNode][0].style.background = '#fff'
                        this.$refs[this.secClickNode][0].style['color'] = '#515775'
                    }
                    this.thirdFlag = false;
                    this.fourthFlag = false;
                    this.fifFlag = false;
                    this.keyValue = node
                    this.matEncode.parentNode = node
                    this.matEncode.level = level + 1
                    this.matEncode.type = type
                    this.secondParNode = this.matEncode.parentNode
                    this.getMatEnCode()
                    this.isAddFlag = true
                    this.secondValue = ''
                    this.thirdValue = ''
                    this.fourthValue = ''
                    this.fifValue = ''
                    this.createLastNode = node
                    this.firClickNode = node
                    this.secClickNode = ''
                    this.thrClickNode = ''
                    this.fourClickNode = ''
                    this.fifClickNode = ''
                    this.isWear = false
                    this.addLiveType.brand = ''
                    // this.createLastNode = this.firstValue.split('-')[0]
                    break
                case 2:
                    // this.matEncode.parentNode = this.secondValue.split('-')[0]
                    // this.matEncode.level = parseInt(this.secondValue.split('-')[1]) + 1
                    // this.matEncode.type = this.secondValue.split('-')[2]
                    this.$refs[node][0].style.background = '#515775'
                    this.$refs[node][0].style['color'] = '#fff'
                    if (this.secClickNode !== node && this.secClickNode !== '') {
                        this.$refs[this.secClickNode][0].style.background = '#fff'
                        this.$refs[this.secClickNode][0].style['color'] = '#515775'
                    }
                    if (this.thrClickNode !== node && this.thrClickNode !== '') {
                        this.$refs[this.thrClickNode][0].style.background = '#fff'
                        this.$refs[this.thrClickNode][0].style['color'] = '#515775'
                    }
                    this.fourthFlag = false;
                    this.fifFlag = false;
                    this.matEncode.parentNode = node
                    this.matEncode.level = level + 1
                    this.matEncode.type = type
                    this.thirdParNode = this.matEncode.parentNode
                    this.getMatEnCode()
                    this.isAddFlag = true
                    this.thirdValue = ''
                    this.fourthValue = ''
                    this.createLastNode = node
                    this.secClickNode = node
                    this.thrClickNode = ''
                    this.fourClickNode = ''
                    this.fifClickNode = ''
                    // this.createLastNode = this.secondValue.split('-')[0]
                    this.fifValue = ''
                    break
                case 3:
                    // this.matEncode.parentNode = this.thirdValue.split('-')[0]
                    // this.matEncode.level = parseInt(this.thirdValue.split('-')[1]) + 1
                    // this.matEncode.type = this.thirdValue.split('-')[2]
                    this.$refs[node][0].style.background = '#515775'
                    this.$refs[node][0].style['color'] = '#fff'
                    if (this.thrClickNode !== '' && this.thrClickNode !== node) {
                        this.$refs[this.thrClickNode][0].style.background = '#fff'
                        this.$refs[this.thrClickNode][0].style['color'] = '#515775'
                    }
                    if (this.fourClickNode !== '' && this.fourClickNode !== node) {
                        this.$refs[this.fourClickNode][0].style.background = '#fff'
                        this.$refs[this.fourClickNode][0].style['color'] = '#515775'
                    }
                    this.fifFlag = false;
                    this.matEncode.parentNode = node
                    this.matEncode.level = level + 1
                    this.matEncode.type = type
                    this.fourthParNode = this.matEncode.parentNode
                    this.getMatEnCode()
                    this.isAddFlag = true
                    this.fourthValue = ''
                    this.createLastNode = node
                    this.thrClickNode = node
                    this.fourClickNode = ''
                    this.fifClickNode = ''
                    // this.createLastNode = this.thirdValue.split('-')[0]
                    this.fifValue = ''
                    break
                case 4:
                    // this.matEncode.parentNode = this.fourthValue.split('-')[0]
                    // this.matEncode.level = parseInt(this.fourthValue.split('-')[1]) + 1
                    // this.matEncode.type = this.fourthValue.split('-')[2]
                    this.$refs[node][0].style.background = '#515775'
                    this.$refs[node][0].style['color'] = '#fff'
                    if (this.fourClickNode !== '' && this.fourClickNode !== node) {
                        this.$refs[this.fourClickNode][0].style.background = '#fff'
                        this.$refs[this.fourClickNode][0].style['color'] = '#515775'
                    }
                    if (this.fifClickNode !== '' && this.fifClickNode !== node) {
                        this.$refs[this.fifClickNode][0].style.background = '#fff'
                        this.$refs[this.fifClickNode][0].style['color'] = '#515775'
                    }
                    this.matEncode.parentNode = node
                    this.matEncode.level = level + 1
                    this.matEncode.type = type
                    this.fifthParNode = this.matEncode.parentNode
                    this.getMatEnCode()
                    this.isAddFlag = true
                    this.createLastNode = node
                    this.fourClickNode = node
                    this.fifClickNode = ''
                    // this.createLastNode = this.fourthValue.split('-')[0]
                    this.fifValue = ''
                    break
                case 5:
                    this.$refs[node][0].style.background = '#515775'
                    this.$refs[node][0].style['color'] = '#fff'
                    if (this.fifClickNode !== '' && this.fifClickNode !== node) {
                        this.$refs[this.fifClickNode][0].style.background = '#fff'
                        this.$refs[this.fifClickNode][0].style['color'] = '#515775'
                    }
                    this.createLastNode = node
                    this.fifClickNode = node
                    // this.createLastNode = this.fifValue.split('-')[0]
                    this.isAddFlag = false
                    break
            }
        },
        addBaseDlogClose(form) {
            this.$refs[form].resetFields();
        },
        addMatEncode(form) {
            this.$refs[form].validate((valid) => {
                if (!valid) return;
                addParMatCode(JSON.stringify(this.addRootCode)).then(res => {
                    if (res.data.code == 0) {
                        this.$message({
                            type: 'success',
                            message: this.$t('newmaterialcategoryadded')
                        })
                        this.addDialogVisible = false
                        this.matEncode.parentNode = this.addRootCode.parentNode
                        this.matEncode.level = this.addRootCode.level
                        this.getMatEnCode()
                    }
                })
            })
        },
        addBaseMatEncode(form) {
            this.$refs[form].validate((valid) => {
                if (!valid) return;
                addParMatCode(JSON.stringify(this.addBaseRootCode)).then(res => {
                    if (res.data.code == 0) {
                        this.$message({
                            type: 'success',
                            message: this.$t('newmaterialcategoryadded')
                        })
                        this.addBaseDialogVisible = false
                        this.matEncode.parentNode = this.addBaseRootCode.parentNode
                        this.matEncode.level = this.addBaseRootCode.level
                        this.getMatEnCode()
                    }
                })
            })
        },
        addDataShow(level) {
            switch (level) {
                case 1:
                    this.addRootCode.level = level;
                    this.addBaseDialogVisible = true
                    break;
                case 2:
                    this.addRootCode.level = level;
                    this.addRootCode.type = this.matEncode.type
                    this.addRootCode.parentNode = this.secondParNode
                    getMaxNumFromChild(JSON.stringify({ node: this.secondParNode })).then(res => {
                        if (res.data.code == 0) {
                            this.addRootCode.code = res.data.data
                        }
                    })
                    this.addDialogVisible=true
                    break;
                case 3:
                    this.addRootCode.level = level;
                    this.addRootCode.type = this.matEncode.type
                    this.addRootCode.parentNode = this.thirdParNode
                    getMaxNumFromChild(JSON.stringify({ node: this.thirdParNode })).then(res => {
                        if (res.data.code == 0) {
                            this.addRootCode.code = res.data.data
                        }
                    })
                    this.addDialogVisible=true
                    break;
                case 4:
                    this.addRootCode.level = level;
                    this.addRootCode.type = this.matEncode.type
                    this.addRootCode.parentNode = this.fourthParNode
                    getMaxNumFromChild(JSON.stringify({ node: this.fourthParNode })).then(res => {
                        if (res.data.code == 0) {
                            this.addRootCode.code = res.data.data
                        }
                    })
                    this.addDialogVisible=true
                    break;
                case 5:
                    this.addRootCode.level = level;
                    this.addRootCode.type = this.matEncode.type
                    this.addRootCode.parentNode = this.fifthParNode
                    getMaxNumFromChild(JSON.stringify({ node: this.fifthParNode })).then(res => {
                        if (res.data.code == 0) {
                            this.addRootCode.code = res.data.data
                        }
                    })
                    this.addDialogVisible=true
                    break;
            }
        },
        addDlogClose(form) {
            this.$refs[form].resetFields();
        },
        createCode() {
            updateParMatCode(JSON.stringify({ node: this.createLastNode })).then(res => {
                if (res.data.code == 0) {
                    this.$message({
                        type: 'success',
                        message: this.$t('creatingasuccessful')
                    })
                    this.showParamFlag = true
                    this.addLiveType.bh = res.data.data
                    this.addLiveType.type = this.matEncode.type
                    if (this.firClickNode == 'C') {
                        this.addLiveType.brand = 'astre'
                        this.isWear = true
                    }
                    this.isAddFlag = true
                }
            })
        },
        saveModeCode() {
            insertMatDetail(JSON.stringify(this.addLiveType)).then(res => {
                if (res.data.code == 0) {
                    this.$message({
                        type: 'success',
                        message: this.$t('sucadd')
                    })
                    this.addMode = false
                    this.addItemDialogVisible = false
                    this.getData()
                }
            })
        },
        // 上传文件超出个数触发
        scotland() {
            this.$message.warning('只能选择一个文件')
        },
        // 图片上传
        handleImg(res) {
            if (res.code == 0) {
                switch (this.doType) {
                    case 1:
                        this.addLiveType.pictureUrl = res.data
                        break
                    case 2:
                        this.modifyDataForm.pictureUrl = res.data
                        break
                }
            }
        },
        hadleRemove(file) {
            if (file) {
                switch (this.doType) {
                    case 1:
                        this.addLiveType.pictureUrl = ''
                        break
                    case 2:
                        this.modifyDataForm.pictureUrl = ''
                        break
                }
            }
        }
    },
    mounted() {
        this.getData()
    }
}
</script>

<style lang="less" scoped>
@import './matDetail.less';
</style>
